<html>
    <head>
        <style>
            body {
                padding: 100px;
                margin: 0;
            }

            #box {
                width: 100px;
                height: 100px;
                background-color: #0077ff;
            }

            [data-layout-correct="false"] {
                background: #dd1144 !important;
                opacity: 1 !important;
            }
        </style>
    </head>
    <body>
        <div id="root"></div>
        <script type="module" src="/src/imports/optimized-appear.js"></script>
        <script type="module" src="/src/imports/script-assert.js"></script>

        <script type="module">
            const {
                motion,
                animate,
                startOptimizedAppearAnimation,
                optimizedAppearDataAttribute,
                motionValue,
                frame,
            } = window.Motion
            const { matchViewportBox } = window.Assert
            const root = document.getElementById("root")

            const duration = 0.5
            const x = motionValue(0)

            let isFirstFrame = true

            function Component() {
                const [top, setTop] = React.useState(0)
                React.useEffect(() => {
                    setTimeout(() => {
                        setTop(100)
                    }, 250)
                }, [])

                return React.createElement(motion.div, {
                    id: "box",
                    initial: { opacity: 0 },
                    animate: { opacity: 1 },
                    transition: {
                        duration,
                        ease: "linear",
                        layout: { duration: 10 },
                    },
                    style: {
                        x,
                        top,
                        position: "relative",
                        background: top ? "red" : "blue",
                    },
                    layout: true,
                    onLayoutAnimationStart: () => {
                        requestAnimationFrame(() => {
                            const box = document.getElementById("box")

                            if (
                                box.style.opacity ===
                                window.getComputedStyle(box).opacity
                            ) {
                                /**
                                 * If style.opacity and computed style.opacity are the same,
                                 * it means the optimised opacity animation was cancelled by
                                 * the layout animation.
                                 */
                                showError(
                                    "style attr and computed style should be slightly different"
                                )
                            }
                        })
                    },
                    [optimizedAppearDataAttribute]: "a",
                    children: "Content",
                })
            }

            // Emulate server rendering of element
            root.innerHTML = ReactDOMServer.renderToString(
                React.createElement(Component)
            )

            // Start WAAPI animation
            const animation = startOptimizedAppearAnimation(
                document.getElementById("box"),
                "opacity",
                [0, 1],
                {
                    duration: duration * 1000,
                    ease: "linear",
                },
                (animation) => {
                    setTimeout(() => {
                        ReactDOMClient.hydrateRoot(
                            root,
                            React.createElement(Component)
                        )
                    }, (duration * 1000) / 4)
                }
            )
        </script>
    </body>
</html>
